﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ListCustomerNameDependent : Form
    {
        private int customerID;

        public ListCustomerNameDependent(int customerID, string descriptionOfficial)
        {
            InitializeComponent();

            this.customerID = customerID;
            this.Text = this.Text + " [" + descriptionOfficial + "]";

            StackedHeaderDecorator stackedHeaderDecoratorAvailable = new StackedHeaderDecorator(this.dataGridCustomerNameTracking);

            DataTable dataTableTracking = SQLDatabase.GetDataTable(" SELECT ListCustomerName.CustomerID, ListCustomerName.Description, ListCustomerName.DescriptionOfficial, ListCustomerName.DescriptionOriginal, ListDistributorName.DescriptionOfficial AS DistributorName, ListCustomerName.ContactName, ListCustomerName.AddressNo, ListCustomerName.DependentOnly FROM ListCustomerNameColumn INNER JOIN ListCustomerName ON ListCustomerNameColumn.ColumnValue = ListCustomerName.DescriptionOriginal LEFT JOIN ListDistributorName ON ListCustomerNameColumn.DistributorID = ListDistributorName.DistributorID WHERE ListCustomerNameColumn.CustomerID = " + this.customerID); //ListCustomerName.DependentOnly = 1 AND

            this.dataGridCustomerNameTracking.AutoGenerateColumns = false;
            this.dataGridCustomerNameTracking.DataSource = dataTableTracking;
        }

        private void dataGridCustomerNameTracking_SelectionChanged(object sender, EventArgs e)
        {
            DataRow dataRowTracking = ((DataRowView)this.dataGridCustomerNameTracking.CurrentRow.DataBoundItem).Row;
            if (dataRowTracking != null && dataRowTracking["CustomerID"] != DBNull.Value && (bool)dataRowTracking["DependentOnly"])
                this.toolStripButtonSplitCustomer.Enabled = true;
            else
                this.toolStripButtonSplitCustomer.Enabled = false;
        }

        private void toolStripButtonEscapeAndSplitCustomer_Click(object sender, EventArgs e)
        {
            if (sender.Equals(this.toolStripButtonSplitCustomer))
            {
                DataRow dataRowTracking = ((DataRowView)this.dataGridCustomerNameTracking.CurrentRow.DataBoundItem).Row;
                if (dataRowTracking != null && dataRowTracking["CustomerID"] != DBNull.Value && (bool)dataRowTracking["DependentOnly"])
                {
                    ListCustomerNameSplit customerNameSplit = new ListCustomerNameSplit(this.customerID, (int)dataRowTracking["CustomerID"]);
                    if (customerNameSplit.ShowDialog() == DialogResult.OK)
                    {
                        customerNameSplit.Dispose();

                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            if (sender.Equals(this.toolStripButtonEscape))
                this.DialogResult = DialogResult.Cancel;
        }

        private void toolStripButtonShowEventLog_Click(object sender, EventArgs e)
        {
            //Call lShowEventLog(ENMVNTaskID.EListCustomerName, Int(Me.vsFMaster.RowData(Me.vsFMaster.RowSel)))
        }



    }
}
